home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- HasRamDisk.c
-
- A dynamic check for the existence of a RAM disk as created by Apple's
- Memory control panel. Since the user can remove the RAM disk at any time
- by turning it off from the Memory control panel, and can rename it at any
- time, the code here needs to be called before assuming the existence of
- a RAM disk. In other words, don't just check once during your application
- startup and assume thereafter that it will still be there, check ideally
- before each access. Program defensively...
-
- history:
-
- modified: xx/xx/xx who are you? what did you do?
- created: 08/10/94 greg poole
-
- Greg Poole
- Vital Images, Inc.
- 505 N. 4th Street
- Fairfield, IA 52556
- (515) 472-7726
- email: greg@vitalimages.com
-
- ******************************************************************************/
-
- #include <string.h>
- #include "HasRamDisk.h"
-
- // this structure is based on the DRVR definition in MPWTypes.r
- //
- struct DRVRresourceRec
- {
- // description of drvrFlags
- //
- // struct
- // {
- // unsigned hiUnused : 1; // unused
- // unsigned needLock : 1; // lock drvr in memory
- // unsigned needTime : 1; // for periodic action
- // unsigned needGoodbye : 1; // call before heap reinit
- // unsigned statusEnable : 1; // responds to status
- // unsigned ctlEnable : 1; // responds to control
- // unsigned writeEnable : 1; // responds to write
- // unsigned readEnable : 1; // responds to read
- // unsigned loUnused : 8; // low byte of drvrFlags word unused
- // } drvrFlags;
-
- short drvrFlags; // flags as defined above
- unsigned short driverDelay; // driver delay (ticks)
- short deskAccEventMask; // desk acc event mask
- short driverMenuID; // driver menu ID
-
- unsigned short offsetOpen; // offset to DRVRRuntime open
- unsigned short offsetPrime; // offset to DRVRRuntime prime
- unsigned short offsetControl; // offset to DRVRRuntime control
- unsigned short offsetStatus; // offset to DRVRRuntime status
- unsigned short offsetClose; // offset to DRVRRuntime close
-
- Str31 driverName; // driver name
- char driverCode[1]; // driver code
- };
- typedef struct DRVRresourceRec DRVRresourceRec;
- typedef DRVRresourceRec *DRVRresourcePtr, **DRVRresourceHndl;
-
- // constants
- //
- const char kDrvrHandleBit = 0x40; // bit 7 of 'DRVR' dCtlFlags signals driver is handle
- // instead of pointer and needs to be locked in memory
- const char kRamDiskName[] = "\p.EDisk"; // Apple's RAM disk driver name
-
-
- // pass in an FSSpecPtr to hold a reference to a RAM disk,
- // returns TRUE if there is currently a RAM disk, FALSE if not
- //
- Boolean HasRamDisk( FSSpecPtr ramDiskSpec )
- {
- Boolean hasRamDisk = FALSE, isHandle = FALSE;
- short whichVol = 1; // start with first disk volume
- HVolumeParam volPB;
- OSErr theErr = noErr, anErr = noErr;
- DCtlHandle dctlHndl = NULL;
- DRVRresourcePtr drvrPtr = NULL;
- DRVRresourceHndl drvrHndl = NULL;
- Ptr aPtr = NULL;
- Str31 volName;
-
- do // test each mounted disk volume
- {
- volPB.ioNamePtr = volName;
- volPB.ioVRefNum = 0; // 0 means use ioVolIndex
- volPB.ioVolIndex = whichVol; // use this to determine volume
-
- if ( ( theErr = PBHGetVInfoSync( (HParmBlkPtr) &volPB ) ) == noErr )
- {
- // get this volume's device control entry from the unit table.
- // do not lock the dctlHndl, I spent a couple of days figuring
- // out that locking this handle causes a crash in the CompServer
- // because it is locked at interrupt time...
- //
- if ( ( dctlHndl = GetDCtlEntry( volPB.ioVDRefNum ) ) != NULL )
- {
- // is the device's driver in a handle or a pointer?
- //
- if ( ( isHandle = (*dctlHndl)->dCtlFlags & kDrvrHandleBit ) != 0 )
- {
- drvrHndl = (DRVRresourceHndl) (*dctlHndl)->dCtlDriver;
- drvrPtr = *drvrHndl;
- }
- else
- drvrPtr = (DRVRresourcePtr) (*dctlHndl)->dCtlDriver;
-
- // get this device's driver, check if it is a RAM disk
- //
- if ( !memcmp( drvrPtr->driverName, kRamDiskName, *kRamDiskName + 1 ) )
- {
- // this driver is the RAM disk driver, create an FSSpec to its root dir
- //
- anErr = FSMakeFSSpec( volPB.ioVRefNum, fsRtDirID, volName, ramDiskSpec );
- if ( anErr == noErr )
- hasRamDisk = TRUE;
- break;
- }
- }
- }
- whichVol++; // go to next volume
- }
- while ( theErr != nsvErr );
-
- return hasRamDisk;
-
- } // end HasRamDisk
-
-
- // define TEST_RAM_DISK for a standalone test
- //
- #define TEST_RAM_DISK
-
- #if defined( TEST_RAM_DISK )
-
- // local function prototypes
- //
- static void InitTheMac( void );
-
- static void InitTheMac( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- MaxApplZone();
-
- } // end InitTheMac
-
- void main( void )
- {
- FSSpec ramDiskSpec;
- Boolean hasRamDisk;
-
- InitTheMac();
- hasRamDisk = HasRamDisk( &ramDiskSpec );
-
- } // end main
-
- #endif // TEST_RAM_DISK
-
-
-